import { _decorator, Collider2D, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('FloorBox')
export class FloorBox extends Component {

    @property([Node])
    doors: Node[] = [];

    @property([Collider2D])
    colliders: Collider2D[] = [];

    isOpen = false;

    start() {
        this.schedule(this.CheckIndex, 0.1);
    }

    update(deltaTime: number) {

    }

    Click() {
        this.isOpen = !this.isOpen;
        this.doors[0].active = !this.isOpen;
        this.doors[1].active = this.isOpen;
        for (let i = 0; i < this.colliders.length; i++) {
            const element = this.colliders[i];
            element.enabled = this.isOpen;
        }
    }

    CheckIndex() {
        let arrY: (Node | number)[][] = []
        for (let i = 0; i < this.node.children.length; i++) {
            const element = this.node.children[i];
            arrY.push([element, element.getPosition().y]);
        }
        arrY.sort((a, b) => { return b[1] as number - (a[1] as number) });
        for (let i = 0; i < arrY.length; i++) {
            const element = arrY[i][0] as Node;
            element.setSiblingIndex(i);
        }
    }
}


